package com.quanshu.exchange.ui.gui.profile;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.change.broadcast.PayResultBroadcatReciver;
import com.quanshu.change.person.view.NjfListActivity;
import com.quanshu.change.view.GoldActivity;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.entity.AccountBalanceEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.shop.base.view.BaseTitleBar;

import java.math.BigDecimal;
import java.math.RoundingMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyWalletActivity extends BaseActivity implements PayResultBroadcatReciver.PayResultSuccessfulListener{


    @BindView(R.id.integral_num)
    TextView integralNum;
    @BindView(R.id.advance_integral)
    RelativeLayout advanceIntegral;
    @BindView(R.id.wallet_title)
    BaseTitleBar walletTitle;
    @BindView(R.id.integra2_num)
    TextView integra2Num;
    @BindView(R.id.advance_integra2)
    RelativeLayout advanceIntegra2;
    @BindView(R.id.integra3_num)
    TextView integra3Num;
    @BindView(R.id.advance_integra3)
    RelativeLayout advanceIntegra3;
    @BindView(R.id.integra4_num)
    TextView integra4Num;
    @BindView(R.id.advance_integra4)
    RelativeLayout advanceIntegra4;
    private PayResultBroadcatReciver payResultBroadcatReciver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);
        initView();
        initListener();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        unregisterReceiver(payResultBroadcatReciver);
    }

    private void initView() {
        payResultBroadcatReciver = new PayResultBroadcatReciver(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PayResultBroadcatReciver.PAY_GOLD_RECHAGR_SUCCESSFUL);
        registerReceiver(payResultBroadcatReciver, intentFilter);
        getBalance();
    }

    private void initListener() {
        ImmersionBar.with(this).titleBar(walletTitle).statusBarColor(R.color.white).statusBarDarkFont(true).init();
        walletTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("我的钱包").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });

        RxViewHelper.getInstance().clicks(advanceIntegral, aVoid -> {
//            Intent intent = new Intent(MyWalletActivity.this, CattleIntegralActivity.class);
//            startActivity(intent);
            Intent intent = new Intent(MyWalletActivity.this, NjfListActivity.class);
            startActivity(intent);
        });

        RxViewHelper.getInstance().clicks(advanceIntegra2, aVoid -> {
            Intent intent = new Intent(MyWalletActivity.this, GoldActivity.class);
            startActivity(intent);
        });
    }

    //获取账户余额
    private void getBalance() {
        if (currentUser != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .getBalance(currentUser.getId())
                    .compose(new BaseTransformer<>(this))
                    .subscribe(new BaseObserver<BaseResponse<AccountBalanceEntity>>() {
                        @Override
                        public void onSuccess(BaseResponse<AccountBalanceEntity> response) {
                            if (response.getCode() == 1) {
                                currentUser.setScore(response.getData().getNiupoint());
                                currentUser.setGp(response.getData().getGP());
                                MyApplication.getInstance().setCurrentUser(currentUser);
                                integralNum.setText(String.valueOf(response.getData().getNiupoint()));
                                integra2Num.setText(String.valueOf(response.getData().getGold_bean()));
                                integra3Num.setText(String.valueOf(response.getData().getTicket()));
                                BigDecimal bigDecimal = new BigDecimal(response.getData().getPayable_amount()).setScale(2, RoundingMode.HALF_UP);
                                integra4Num.setText(String.valueOf(bigDecimal.doubleValue()));

                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LogUtil.printException(e);
                        }
                    });
        }
    }

    @Override
    public void paySuccess() {
        getBalance();
    }
}
